package com.newear.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.newear.MyApplication;
import com.newear.R;
import com.newear.happyDialog.CamaraCheckDialog;
import com.newear.util.HardWare;

import java.util.ArrayList;

public class HappyMultiSelectActivity extends AppCompatActivity {
    private static final String TAG = "HappyMultiSelectActivit";
    private LinearLayout lL_multi_select_container;
    private Button btn_multi_pre;
    private Button btn_multi_next;
    private ArrayList mMultiTextList;
    private ArrayList mButtonTotalTextList;
    private ArrayList mFirstQuestionList;
    private ArrayList mSecondQuestionList;
    private ArrayList mThirdQuestionList;
    private ArrayList mFourthQuestionList;
    private ArrayList mFiveQuestionList;
    private ArrayList mSixthQuestionList;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_happy_multi_select);
        init();
        findViews();
        setListeners();
        addMultiBtnViews();
    }


    private void init(){
        HardWare.registerActivity(this);
        mContext = this;
        mMultiTextList = new ArrayList();
        mMultiTextList.add("1、您平时容易出汗或晚上睡觉易出汗吗？");
        mMultiTextList.add("2、您平时小便怎么样？");
        mMultiTextList.add("3、您平时大便怎么样？");
        mMultiTextList.add("4、您平时觉得口中有异样味道吗？");
        mMultiTextList.add("5、您平时身体有腹胀不适吗？");
        mMultiTextList.add("6、您平时容易怕冷，怕热，或手足心热吗？");

        mButtonTotalTextList = new ArrayList();

        mFirstQuestionList = new ArrayList();
        mFirstQuestionList.add("活动出汗-夜间出汗");
        mFirstQuestionList.add("正常-占位");//

        mSecondQuestionList = new ArrayList();
        mSecondQuestionList.add("偏黄" + "-" + "  " + "清长(色清、量多)" +"  ");
        mSecondQuestionList.add("夜尿频多-正常");

        mThirdQuestionList = new ArrayList();
        mThirdQuestionList.add("偏干-偏黏");
        mThirdQuestionList.add("  " + "偏稀(或大便溏)" + "  " +  "-" + "正常");

        mFourthQuestionList = new ArrayList();//
        mFourthQuestionList.add("甜-苦");
        mFourthQuestionList.add("黏-口淡");
        mFourthQuestionList.add("正常-占位");

        mFiveQuestionList = new ArrayList();
        mFiveQuestionList.add("乳房胀-食后腹胀");
        mFiveQuestionList.add("胸胁胀满-无");

        mSixthQuestionList = new ArrayList();
        mSixthQuestionList.add("  " + "怕冷(或手足冰凉)" + "  " + "-" + "怕热 ");
        mSixthQuestionList.add("手足心热-正常");

        mButtonTotalTextList.add(mFirstQuestionList);
        mButtonTotalTextList.add(mSecondQuestionList);
        mButtonTotalTextList.add(mThirdQuestionList);
        mButtonTotalTextList.add(mFourthQuestionList);
        mButtonTotalTextList.add(mFiveQuestionList);
        mButtonTotalTextList.add(mSixthQuestionList);

        if (MyApplication.sCustomerInfo.multiSelect1 == null) {
            MyApplication.sCustomerInfo.multiSelect1 = new int[mButtonTotalTextList.size()];
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        HardWare.unregisterActivity(this);
    }

    private void findViews(){
        lL_multi_select_container = (LinearLayout) findViewById(R.id.lL_multi_select_container);
        btn_multi_pre = (Button) findViewById(R.id.btn_multi_pre);
        btn_multi_next = (Button) findViewById(R.id.btn_multi_next);
    }

    private void setListeners(){
        btn_multi_pre.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        btn_multi_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean hasUnSelected = false;
                if (MyApplication.sCustomerInfo.multiSelect1 != null) {
                    for (int i = 0; i < MyApplication.sCustomerInfo.multiSelect1.length; i++) {
                        if (MyApplication.sCustomerInfo.multiSelect1[i] == 0) {
                            hasUnSelected = true;
                            break;
                        }
                    }
                    if (hasUnSelected) {
                        CamaraCheckDialog camaraCheckDialog = new CamaraCheckDialog(mContext, R.style.dialog, "请先完成所有选项", "再进入下一页", -1);
                        camaraCheckDialog.show();
                        return;
                    }
                }
                //跳转最后的多选页面
                Intent intent = new Intent(HappyMultiSelectActivity.this, HappyLastMultiSelectActivity.class);
                startActivity(intent);
            }
        });
    }

    private void addMultiBtnViews(){

        if (lL_multi_select_container != null){
            lL_multi_select_container.removeAllViews();
        }

        for (int i = 0; i < mMultiTextList.size(); i++) {
            View view = View.inflate(this, R.layout.activity_happy_multi_btn_layout, null);
            TextView tvAnswer = (TextView) view.findViewById(R.id.tv_multi_answer_des);
            tvAnswer.setText((String)mMultiTextList.get(i));
            LinearLayout lL_muti_btns = (LinearLayout) view.findViewById(R.id.lL_muti_btns);
            //下面这个是不同问题下button的动态集合
            ArrayList buttonTextList = (ArrayList) mButtonTotalTextList.get(i);
            for (int j = 0; j < buttonTextList.size(); j++) {
                View buttonView = View.inflate(this, R.layout.happy_btn_multi_layout, null);
                final Button leftButton = (Button) buttonView.findViewById(R.id.btn_multi_left);
                final Button rightButton = (Button) buttonView.findViewById(R.id.btn_multi_right);
                if (MyApplication.sCustomerInfo.multiSelect1[i] > 0) {
                    int leftIndex = j * 2 + 1;
                    int rightIndex = j * 2 + 2;
                    if ((MyApplication.sCustomerInfo.multiSelect1[i] + "").contains(leftIndex + "")) {
                        leftButton.setSelected(true);
                    }
                    if ((MyApplication.sCustomerInfo.multiSelect1[i] + "").contains(rightIndex + "")) {
                        rightButton.setSelected(true);
                    }

                }
                //对每个button赋值
                String buttonText = (String) buttonTextList.get(j);
                String[] textTitle = buttonText.split("-");
                leftButton.setText(textTitle[0]);
                rightButton.setText(textTitle[1]);
                if (textTitle[1].equals("占位")){
                    rightButton.setVisibility(View.INVISIBLE);
                }else {
                    rightButton.setVisibility(View.VISIBLE);
                }
                buttonView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                lL_muti_btns.addView(buttonView);

                final int finalJ = j;
                final int finalI = i;
                leftButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // 第i道题的结果 eg: 12
                        int result = MyApplication.sCustomerInfo.multiSelect1[finalI];
                        Log.e(TAG, "result:" + result);
                        // 选中
                        if (!leftButton.isSelected()) {
                            int index = finalJ * 2 + 1;
                            int newResult = result * 10 + index;
                            MyApplication.sCustomerInfo.multiSelect1[finalI] = HardWare.getSort(newResult);
                            leftButton.setSelected(true);
                        } else {
                            if (result < 10) {
                                // 只选择了1个
                                MyApplication.sCustomerInfo.multiSelect1[finalI] = 0;
                            } else {
                                // 已经选择多个
                                // 选择的序号，从1开始
                                int index = finalJ * 2 + 1;
                                MyApplication.sCustomerInfo.multiSelect1[finalI] = Integer.parseInt((result + "").replace(index + "", ""));
                            }
                            // 取消选择
                            leftButton.setSelected(false);
                        }

                    }
                });

                rightButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // 第i道题的结果 eg: 12
                        int result = MyApplication.sCustomerInfo.multiSelect1[finalI];
                        Log.e(TAG, "result:" + result);
                        // 选中
                        if (!rightButton.isSelected()) {
                            int index = finalJ * 2 + 2;
                            int newResult = result * 10 + index;
                            MyApplication.sCustomerInfo.multiSelect1[finalI] = HardWare.getSort(newResult);
                            rightButton.setSelected(true);
                        } else {
                            if (result < 10) {
                                // 只选择了1个
                                MyApplication.sCustomerInfo.multiSelect1[finalI] = 0;
                            } else {
                                // 已经选择多个
                                // 选择的序号，从1开始
                                int index = finalJ * 2 + 2;
                                MyApplication.sCustomerInfo.multiSelect1[finalI] = Integer.parseInt((result + "").replace(index + "", ""));
                            }
                            // 取消选择
                            rightButton.setSelected(false);
                        }
                    }
                });

            }

            //下面是外层的问题
            view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            lL_multi_select_container.addView(view);
        }
    }


}
